package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.product.biz.model.ProductBO;
import com.meituan.catering.management.product.biz.model.request.SearchProductBizRequest;
import com.meituan.catering.management.product.dao.model.ProductDO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/5/19 15:50
 */
public class PageBOConverter {
    public static final Integer DEFAULT_PAGE_SIZE = 10;

    public static PageBO<ProductBO> toPageBO(List<ProductDO> list, SearchProductBizRequest request, Integer countAll) {
        if (CollectionUtils.isEmpty(list) || Objects.isNull(request)) {
            return null;
        }
        PageBO<ProductBO> pageBO = new PageBO<>();
        pageBO.setPageIndex(request.getPageIndex());
        pageBO.setPageSize(request.getPageSize());
        pageBO.setTotalPageCount(totalPageCount(countAll));
        pageBO.setTotalCount(countAll);
        pageBO.setRecords(toProductBOList(list));
        return pageBO;
    }

    public static List<ProductBO> toProductBOList(List<ProductDO> productDOS) {
        if (CollectionUtils.isEmpty(productDOS)) {
            return Collections.emptyList();
        }
        List<ProductBO> productBOS = Lists.newLinkedList();
        for (ProductDO productDO : productDOS) {
            productBOS.add(ProductBOConverter.toProductBO(productDO));
        }
        return productBOS;
    }

    public static Integer totalPageCount(Integer countAll) {
        if (countAll % DEFAULT_PAGE_SIZE == 0) {
            return countAll / DEFAULT_PAGE_SIZE;
        }
        return countAll / DEFAULT_PAGE_SIZE + 1;
    }
}
